package com.woniuxy.yogaapp.service.imp;

import java.util.List;
import javax.annotation.Resource;

import com.woniuxy.yogaapp.dto.EvaluateDto;
import com.woniuxy.yogaapp.dto.UidDto;
import com.woniuxy.yogaapp.mapper.*;
import com.woniuxy.yogaapp.pojo.Chat;
import com.woniuxy.yogaapp.pojo.ClubCoach;
import com.woniuxy.yogaapp.util.AdminPhone;
import com.woniuxy.yogaapp.util.TimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.woniuxy.yogaapp.pojo.Chat;
import com.woniuxy.yogaapp.pojo.Coach;
import com.woniuxy.yogaapp.service.ChatService;
import com.woniuxy.yogaapp.service.CoachService;
import com.woniuxy.yogaapp.util.AdminPhone;
import com.woniuxy.yogaapp.util.SMSUtil;
import lombok.Data;
import com.woniuxy.yogaapp.dto.Check;

import java.util.Date;
import java.util.UUID;

@Service
@Data
@Transactional
public class CoachServiceImp implements CoachService {
	@Resource
	private CoachMapper coachMapper;
	@Resource
	private ChatService chatService;
	@Resource
	private UserMapper userMapper;
	@Resource
	private OrderMapper orderMapper;
	@Resource
	private ChatMapper chatMapper;
	@Resource
	private ClubCoachMapper clubCoachMapper;
	@Resource
	private UserRolesMapper userRolesMapper;


	// 查询所有的教练
	@Override
	public List<Coach> findAllCoach() {
		return coachMapper.findAllCoach();
	}

	// 通过教练id 查询该教练
	@Override
	public Coach findByCid(int cid) {
		return coachMapper.findByCid(cid);
	}

	// 通过教练id查询该教练
	@Override
	public Coach findCoachByUid(Coach coach) {

		return coachMapper.findCoachByUid(coach);
	}

	// 教练老用户 修改个人信息
	@Override
	public int updateCoachInfo(Coach coach) {
		return coachMapper.updateCoachInfo(coach);
	}

	// 新教练添加 个人信息
	@Override
	public String addCoachInfo(Coach coach) {
		String result = "添加失败！";
		int row = coachMapper.addCoachInfo(coach);
		if (row == 1) {
			result = "添加成功!";
		}
		return result;
	}

	@Override
	public String approvePower(Coach coach) {
		String result = "认证请求发送失败！";
		// 添加 教练为认证信息待 管理员认证
		int row = coachMapper.approvePower(coach);

		Coach coach1 = coachMapper.findCoachByUid(coach);
		// 给管理员发通知推送消息
		int row1 = chatService.coachToAdmin(coach1);
		// 给管理员发送短信
		String phoneNumber = AdminPhone.getAdminTel();
		String[] params = { coach1.getCname(), coach1.getCgenre() + "流派教练" };
		SMSUtil.addInformToAdmin(phoneNumber, params);
		if (row == 1 && row1 == 1) {
			result = "认证请求发送成功！";
		}
		return result;
	}

	@Override
	public String repostOrder(Check check) {
		String result ="发送失败";
		String bb = new Date().getTime() + "" + (Math.floor(Math.random() * 90) + 10);
		String aa = bb.substring(0, 15);


		check.setTransctionid(aa);



		check.setTransctionid(aa);
		check.setOtime(new Date());

		int b = coachMapper.reCreateOrder(check);
		//通知学员教练已经确认订单再次

		Chat chat=new Chat();
		chat.setRecipient(userMapper.selectUidBysid(new EvaluateDto(check.getSid(),check.getCid())).getUid());
		chat.setSender(userMapper.selectUidBycid(new EvaluateDto(check.getSid(),check.getCid())).getUid());
		chat.setMestime(TimeUtil.getNowDate());
		chat.setState("未读");
		chat.setMestype(8);
		chat.setMes("又上完课了记得支付噢");
        /*System.out.println("--------");
        System.out.println(chat);*/
		int i1=orderMapper.insertChat(chat);
		if(i1>0){
			result="发送成功";
		}
		return result;

	}

	@Override
	public String updateOrderCheck(Check c) {
		String bb = new Date().getTime() + "" + (Math.floor(Math.random() * 90) + 10);
		String aa = bb.substring(0, 15);


		c.setTransctionid(aa);
		c.setOtime(new Date());
		int b = coachMapper.updateOrderCheck(c);
		//在这里绑定学员和教练的关系插入cid和sid
		//先检测cid和sid是否已经绑定了,绑定了就不用再插入
		UidDto C=coachMapper.selectIsExist(c);
		if(C==null){
			coachMapper.insertCidSid(c);
		}


		//通知学员教练已经确认订单
		Chat chat=new Chat();
		chat.setRecipient(userMapper.selectUidBysid(new EvaluateDto(c.getSid(),c.getCid())).getUid());
		chat.setSender(userMapper.selectUidBycid(new EvaluateDto(c.getSid(),c.getCid())).getUid());
		chat.setMestype(8);
		chat.setMestime(TimeUtil.getNowDate());
		chat.setState("未读");
		chat.setMes("教练已经同意你的请求，请按时上课偶~");
		System.out.println("--------");
		System.out.println(chat);
		int i1=orderMapper.insertChat(chat);

		if (i1 > 0) {
			return "确认成功";
		}
		return "确认失败";
	}

	@Override
	public int updateOrder(Check c) {
		System.out.println(c);
		System.out.println(coachMapper.updateOrder(c));

		return coachMapper.updateOrder(c);
	}

	@Override
	public String postOrder(Check check) {
		String result = "失败";
		//1.改变数据库状态
		int i = coachMapper.updateOrder(check);

		if (i > 0) {
			//2.发送给会员发送通知
			Chat chat=new Chat();
			chat.setRecipient(userMapper.selectUidBysid(new EvaluateDto(check.getSid(),check.getCid())).getUid());
			chat.setSender(userMapper.selectUidBycid(new EvaluateDto(check.getSid(),check.getCid())).getUid());
			chat.setMestype(8);
			chat.setState("未读");
			chat.setMestime(TimeUtil.getNowDate());
			chat.setMes("已经上完课了记得支付噢");
			System.out.println("--------");
			System.out.println(chat);
			int i1=orderMapper.insertChat(chat);
			if(i1>0){
				result="发送成功";
			}


		}
		return result;
	}


	// 查询未确认的教练认证消息
	@Override
	public List<Coach> selectApprove() {
		List<Coach> approvemes = coachMapper.findMes();
		return approvemes;
	}

	// 同意教练认证
	@Override
	public String agreeCoach(Chat chat) {
		String result = "请求失败";
		int cuid = chat.getSender();
		int chatid = chat.getChatid();
		int row = coachMapper.agreeCoach(cuid);
		if (row > 0) {
			chat.setDeal("同意");
			int row2 = chatMapper.changeDealAgree(chatid);
			int row4 = userRolesMapper.deleteRoles1(cuid);
			// 增加通知消息
			int sender = 1;
			int recipient = cuid;
			String mes = "已同意认证";
			String state = "未读";
			String mestime = TimeUtil.getNowDate();
			int mestype = 2;
			Chat chat2 = new Chat(sender, recipient, mes, state, mestime, mestype);
			int row5 = chatMapper.addChat(chat2);
			if (row2 > 0 && row4 > 0 && row5 > 0) {
				int row3 = coachMapper.setCoach(cuid);
				int row6 = chatMapper.changeStatetoReady(chatid);
				if (row3 > 0 && row6 > 0) {
					result = "请求成功，已同意教练认证";
				}
			}
		}
		return result;
	}

	// 驳回教练认证
	@Override
	public String disagreeCoach(Chat chat) {
		String result = "请求失败";
		int cuid = chat.getSender();
		int chatid = chat.getChatid();
		int row = coachMapper.disagreeCoach(cuid);
		if (row > 0) {
			chat.setDeal("驳回");
			int row2 = chatMapper.changeDealDisAgree(chatid);
			// 增加通知消息
			int sender = 1;
			int recipient = cuid;
			String mes = "已驳回认证";
			String state = "未读";
			String mestime = TimeUtil.getNowDate();
			int mestype = 2;
			Chat chat2 = new Chat(sender, recipient, mes, state, mestime, mestype);
			int row3 = chatMapper.addChat(chat2);
			int row6 = chatMapper.changeStatetoReady(chatid);
			if (row2 > 0 && row3 > 0 && row6 > 0) {
				result = "请求成功，已驳回教练认证";
			}
		}
		return result;
	}

	@Override
	public Coach findOneCoach(int uid) {
		return coachMapper.findCoachbyUid(uid);
	}

	@Override
	public List<String> showClubName(int cuid) {
		List<String> result = coachMapper.findClubNameByUid(cuid);
		return result;
	}

	// 教练请求加入某场馆
	@Override
	public String applyForClub(ClubCoach clubCoach) {
		String result = "请求失败！";
		// 1.添加信息到 ClubCoach 表
		int row = clubCoachMapper.applyForClub(clubCoach);
		// 2.发起请求给对应场馆
		String mes = "瑜伽教练：" + clubCoach.getCname() + ",申请加入：" + clubCoach.getYname() + "场馆,请处理!";
		String mestime = TimeUtil.getNowDate();
		Chat chat = new Chat(0, clubCoach.getCuid(), clubCoach.getYuid(), mes, "未读", mestime, 10);
		int row1 = chatMapper.addChat(chat);
		if (row == 1 && row1 == 1) {
			result = "请求发送成功！";
		}
		return result;
	}

}
